package com.jinuo.mhwang.serialport;

import com.jetinno.utils.LogUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import android_serialport_api.SerialPort;

import static android.system.OsConstants.O_NOCTTY;
import static android.system.OsConstants.O_NONBLOCK;
import static android.system.OsConstants.O_RDWR;

public abstract class BaseSerialPort {

    public static final String SERIAL_UNCONNECT = "异常";

    int DEFAULT_PORT_FLAG = O_RDWR | O_NONBLOCK | O_NOCTTY;

    public InputStream mInputStream = null;
    public OutputStream mOutputStream = null;
    SerialPort mSerialPort = null;
    Map<String, String> mSerialMap;
    Map<File, InputStream> serialPortInputStream = new HashMap<File, InputStream>();
    Map<File, OutputStream> serialPortOutputStream = new HashMap<File, OutputStream>();

    public abstract boolean write(String data);

    public abstract boolean write(byte[] data);

    public abstract String receive();

    public abstract byte[] receiveBytes();

    public abstract boolean open();

    public abstract void close();

    /**
     * 读取数据
     *
     * @param inputStream 串口对象
     * @return byte[]数组，若没有,返回null
     */
    public  byte[] readData(InputStream inputStream) {
//        //暂停200毫秒以等待数据返回 有些串口延迟会出现粘包，比如门卡板，因此根据实际情况添加
//        SystemClock.sleep(200);
        int size;
        try {
            if (inputStream == null) {
                return null;
            }
            byte[] buffer = new byte[256];
            size = inputStream.read(buffer);
            byte[] data = new byte[size];
            System.arraycopy(buffer, 0, data, 0, size);
            if (size > 0) {
                return data;
            } else {
                LogUtils.d("BaseSerialPort", "receive no data");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 打开串口
     *
     * @param device   要打开的串口文件
     * @param baudRate 波特率
     * @param flag     标志
     * @return true, 打开成功，false，打开失败
     */
    public  boolean openSerialPort(File device, int baudRate, int flag) {
        try {
            mSerialPort = new SerialPort(device, baudRate, flag);
            mInputStream = mSerialPort.getInputStream();
            mOutputStream = mSerialPort.getOutputStream();
            serialPortInputStream.put(device, mInputStream);
            serialPortOutputStream.put(device, mOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
            mInputStream = null;
            mOutputStream = null;
            LogUtils.logXlog("BaseSerialPort", "串口打开失败" + e.toString());
            return false;
        }
        return mInputStream != null && mOutputStream != null;
    }

    /**
     * 关闭指定端口
     *
     * @param device
     * @return
     */
    public void closeSerialPort(File device) {
        try {
            //关闭端口输入流
            InputStream inputStream = serialPortInputStream.get(device);
            inputStream.close();
            serialPortInputStream.remove(device);
            //关闭端口输出流
            OutputStream outputStream = serialPortOutputStream.get(device);
            outputStream.close();
            serialPortOutputStream.remove(device);

            mSerialPort.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    boolean closeSerialPort() {
//        try {
//            if (mInputStream != null){
//                mInputStream.close();
//                mInputStream = null;
//            }
//            if (mOutputStream != null){
//                mOutputStream.close();
//                mOutputStream = null;
//            }
//            if (mSerialPort != null){
//                mSerialPort.close();
//                mSerialPort = null;
//            }
//        }catch (Exception e){
//            return false;
//        }
//        return true;
//    }
}
